//
//  NotesDetailTableViewCell.m
//  TravelFirst
//
//  Created by lanou on 15/9/21.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesDetailTableViewCell.h"

@implementation NotesDetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundV = [[UIView alloc] init];
        self.backgroundV.backgroundColor = [UIColor whiteColor];
        self.backgroundV.layer.masksToBounds = YES;
        self.backgroundV.layer.cornerRadius = 8;
        [self.contentView addSubview:self.backgroundV];
        
        self.pictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (kWidth - 20) * kFrameWidthScale, 210 * kFrameHeightScale)];
        self.pictureImageView.backgroundColor = [UIColor blueColor];
        self.pictureImageView.userInteractionEnabled = YES;
        self.pictureImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.pictureImageView.clipsToBounds = YES;
        self.pictureImageView.layer.masksToBounds = YES;
        self.pictureImageView.layer.cornerRadius = 8;
        [self.backgroundV addSubview:self.pictureImageView];
        
        self.praiseAndCommentView = [[PraiseAndCommentView alloc] initWithFrame:CGRectMake((self.pictureImageView.width - 150) * kFrameWidthScale, (self.pictureImageView.height - 15 - 44) * kFrameHeightScale, 150 * kFrameWidthScale, 44 * kFrameHeightScale)];
        self.praiseAndCommentView.backgroundColor = [UIColor purpleColor];
        [self.pictureImageView addSubview:self.praiseAndCommentView];
        
        self.descriptionLabel = [[UILabel alloc] init];
        self.descriptionLabel.font = [UIFont systemFontOfSize:13];
        self.descriptionLabel.numberOfLines = 0;
        //self.descriptionLabel.backgroundColor = [UIColor blueColor];
        [self.backgroundV addSubview:self.descriptionLabel];
        
        self.timeImageView = [[UIImageView alloc] init];
        self.timeImageView.image = [UIImage imageNamed:@"icon_time_20x20"];
        [self.backgroundV addSubview:self.timeImageView];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.font = [UIFont systemFontOfSize:13];
        [self.backgroundV addSubview:self.timeLabel];
        
        self.backgroundV.frame = CGRectMake(10 * kFrameWidthScale, 10 * kFrameHeightScale, (kWidth - 20) * kFrameWidthScale, self.pictureImageView.bottom * kFrameHeightScale);
    }
    return self;
}

+ (CGRect)setFrameWithString:(NSString *)string
{
    CGRect frame = [string boundingRectWithSize:CGSizeMake(kWidth - 30, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName] context:nil];
    return frame;
}

- (void)setDescriptionLabelFrame:(CGRect)frame string:(NSString *)string
{
    CGRect desFrame = [NotesDetailTableViewCell setFrameWithString:string];
    self.descriptionLabel.frame = CGRectMake(frame.origin.x * kFrameWidthScale, frame.origin.y * kFrameHeightScale, frame.size.width * kFrameWidthScale, desFrame.size.height * kFrameHeightScale);
    self.descriptionLabel.text = string;
}

- (void)setTimeLabelWithFrame:(CGRect)frame string:(NSString *)string
{
    self.timeImageView.frame = CGRectMake(frame.origin.x * kFrameWidthScale, frame.origin.y * kFrameHeightScale, frame.size.width * kFrameWidthScale, frame.size.height * kFrameHeightScale);
    self.timeLabel.frame = CGRectMake((frame.origin.x + frame.size.width + 5) * kFrameWidthScale, frame.origin.y * kFrameHeightScale, (kWidth - 30 - frame.size.width - 5) * kFrameWidthScale, 20);
    self.timeLabel.text = [self transformDateWithDateString:string];
}

- (NSString *)transformDateWithDateString:(NSString *)dateString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateString floatValue]];
    NSString *string = [[formatter stringFromDate:date] substringToIndex:16];
    return string;
}

- (void)setValueWithModel:(NotesModel *)model string:(NSString *)string
{
    [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:nil];
    if ([string isEqualToString:@"NotesDetailViewController"]) {
        [self.praiseAndCommentView setValueWithPraiseStr:model.fav_count commentStr:model.comment_count];
    } else if ([string isEqualToString:@"NotesCommentTableViewController"]) {
        self.praiseAndCommentView.alpha = 0;
    }
    
    BOOL isPosts_description = model.posts_description && model.posts_description.length != 0;
    if (isPosts_description) {
        [self setDescriptionLabelFrame:CGRectMake(5, 225, kWidth - 30, 100) string:model.posts_description];
        [self setTimeLabelWithFrame:CGRectMake(5, self.descriptionLabel.bottom + 10, 20, 20) string:model.day];
    } else {
        self.descriptionLabel.frame = CGRectZero;
        [self setTimeLabelWithFrame:CGRectMake(5, 225, 20, 20) string:model.day];
    }
    
    self.backgroundV.frame = CGRectMake(10 * kFrameWidthScale, 10 * kFrameHeightScale, (kWidth - 20) * kFrameWidthScale, self.timeLabel.bottom + 5);
}

- (void)setTarget:(id)target praiseAction:(SEL)praiseAction commentAction:(SEL)commentAction index:(NSInteger)index
{
    [self.praiseAndCommentView setTarget:target praiseAction:praiseAction commentAction:commentAction index:index];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
